package framework.manager;

import haxe.ds.StringMap;
import pixi.loaders.Loader;
import haxe.ds.HashMap;
import haxe.Json;
import js.html.XMLHttpRequest;


class ResourceManager{

	public static var instance:ResourceManager = new ResourceManager();

	private var _dataCacha:StringMap<Dynamic>;

    private function new() {
		_dataCacha = new StringMap<Dynamic>();
    }

	public function initialise():Void{
//		FactoryManager.instance.registerFactory("");
	}

	/**
	*	加载资源文件
	**/
	public function loadDataFile(src:String, ?async:Bool = false){
		var progress:Float = 0;
		var xobj = new XMLHttpRequest();
		xobj.open("GET", src, async);

		xobj.onprogress = function(e:Dynamic) {
			progress = e.lengthComputable ? e.loaded / e.total : 0;
			if (progress > 1) progress = 1;
		};

		xobj.onload = function() {
			if (xobj.status < 400) {
				_dataCacha.set(src, Json.parse(xobj.responseText));
			}
		};
		xobj.send(null);
	}

	/**
	*	加载网络资源
	**/
	public function loadAssets(name:String, onLoad:String->Void):Void{
		var loader = new Loader();
		loader.baseUrl = "assets/";
		loader.add(name);

		loader.on("progress", function(){
			trace(loader.progress);
		});
		loader.load(function(){
			trace(loader.resources.get(name).data);
		});
	}

	/**
	*	加载json数据
	**/
	public function loadData(name:String):Dynamic{
		var js = new StringBuf();
		js.add("require ('");
		js.add(name);
		js.add("')");
		return untyped __js__(js.toString());
	}



}
